<template>
  <van-nav-bar
    title="账单"
    left-arrow
    @click-left="onClickLeft"
    class="custom-nav-bar"
  />

  <!-- 月份和收支概览 -->
  <div class="month-overview">
    <div class="spending-income">
       
      <div class="spending">
        <div class="label-amount">
          <span class="label">支出</span>
          <span class="amount">¥{{ totalSpending }}</span>
        </div>
      </div>
      <div class="income">
        <div class="label-amount">
          <span class="label">收入</span>
          <span class="amount">¥{{ totalIncome }}</span>
        </div>
      </div>
      <div class="leftamount">
        <div class="label-amount">
          <span class="label">合计</span>
          <span class="amount">¥{{ totalIncome - totalSpending }}</span>
        </div>
      </div>
    </div>
  </div>

  <!-- 添加一个占位符 div -->
  <div class="month-overview-placeholder"></div>

  <div class="transaction-list">
    <!-- 账单记录列表 -->
    <ul class="record-list">
        <van-swipe-cell  v-for="(record, index) in transactions" :key="record.id">
            <li class="record-item">
                <div class="record-icon">
                <img :src="record.icon" alt="Record Icon" />
                </div>
                <div class="record-details">
                <div class="record-title">{{ record.title }}</div>
                <div class="record-time">{{ record.time }}</div>
                </div>
                <div class="record-amount-balance">
                    <div class="record-amount">{{ record.amount }}</div>
                    <div class="record-balance">余额: {{ record.balance }}</div>
                </div>
            </li>
            <template #right>
                <van-button 
                    v-if="record.backType === 1"
                square type="danger" 
                class="delete-button"
                @click="handleRefund(record, index)"
                text="退回" />
            </template>
        </van-swipe-cell>
      
    </ul>
  </div>
</template>
  
  <script>

import {get} from '../../utils/request';
import { showToast, showDialog } from 'vant';

export default {
  data() {
    return {
      transactions: [],
      totalSpending: 0,
      totalIncome: 0,
    };
  },
  created() {
    this.fetchTransactions();
  },
  methods: {
    handleRefund(record, index) {
      // 在这里处理退回逻辑
      if(record.backType !== 1) {
        showToast('不支持退款的记录');
        return
      }
      showDialog({
        title: '确认退回',
        message: `确定要退回 ¥${record.amount} 的交易吗？`,
      }).then(() => {
        // 用户点击确认
        this.refundTransaction(record.id, index);
      }).catch(() => {
        // 用户点击取消
        console.log('用户取消了退回操作');
      });
    },
    async fetchTransactions() {
      try {
        const res = await get('/point/queryTransactionInfo');
        const data = res.data
        console.log(data);
        // 计算总支出和总收入
        let spending = 0;
        let income = 0;

        this.transactions = data.map((record) => {
          if (record.amount < 0) {
            spending += Math.abs(record.amount);
          } else {
            income += record.amount;
          }
          return {
            id: record.id,
            icon: record.icon,
            title: record.title,
            time: record.time,
            amount: record.amount,
            balance: record.endBalance,
            assetsCode: record.assetsCode,
            backType: record.backType,
          };
        });

        this.totalSpending = spending.toFixed(2);
        this.totalIncome = income.toFixed(2);
      } catch (error) {
        console.error("Error fetching transactions:", error);
      }
    },
     async refundTransaction(transactionId, index) {
      try {
        // 调用退回API
        const response = await get(`/point/refund/${transactionId}`);
        this.fetchTransactions();
      } catch (error) {
        
      }
    },
    onClickLeft() {
      // 使用 Vue Router 的 back 方法返回上一个界面
      this.$router.back();
    },
  },
};
</script>
  
<style scoped>
.custom-nav-bar :deep(.van-icon-arrow-left) {
  color: black;
}
.custom-nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.month-overview {
  position: fixed;
  top: 46px; /* 导航栏的高度，可能需要调整 */
  left: 0;
  right: 0;
  z-index: 999;
  background-color: #ffffff;
  padding: 16px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.month-overview-placeholder {
  height: 130px; /* 根据 month-overview 的实际高度调整 */
}

.spending,
.income {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.label-amount {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.month-text {
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.month-arrow {
  margin-left: 4px;
  color: #666;
}

.spending-income {
  display: flex;
  justify-content: space-between;
}

.spending,
.income {
  display: flex;
  flex-direction: column;
}

.label {
  font-size: 14px;
  color: #666;
  margin-bottom: 4px;
}

.amount {
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.spending .amount {
  color: #ff4d4f;
}

.income .amount {
  color: #52c41a;
}

.leftamount .amount {
  color: #1890ff;
}


.transaction-list {
  background-color: #ffffff;
  border-radius: 12px;
  margin: 12px;
  padding: 12px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.record-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.record-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  position: relative;
  border-bottom: 1px solid #f0f0f0;
}

.record-item:last-child {
  border-bottom: none;
}

.record-icon {
  position: relative;
  z-index: 2;
}

.record-icon img {
  width: 40px;
  height: 40px;
  margin-right: 12px;
  border-radius: 50%;
  background-color: #ffffff;
}

.record-details {
  flex: 1;
}

.record-title {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 4px;
}

.record-time {
  font-size: 12px;
  color: #999;
}

.record-amount {
  font-size: 16px;
  font-weight: 500;
}
.record-amount-balance {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.record-balance {
  font-size: 12px;
  color: #999;
  margin-top: 4px;
}
.delete-button {
    height: 100%;
  }
</style>